package com.chenhongjian.webapp.service.impl;

import com.chenhongjian.webapp.bean.TbUser;
import com.chenhongjian.webapp.dao.TbUserMapper;
import com.chenhongjian.webapp.service.IUserService;
import com.chenhongjian.webapp.utils.AccountValidatorUtil;
import com.chenhongjian.webapp.utils.FiledFilter;
import com.chenhongjian.webapp.utils.RandCodeUtils;
import com.chenhongjian.webapp.utils.SmsCodeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.thymeleaf.util.StringUtils;

import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * Created by a on 2018/3/27.
 */
@Service
public class IUserServiceImpl implements IUserService{


    @Autowired
    TbUserMapper userDao;
    @Override
    public int sendSmscode(String phone) {
        return StringUtils.isEmpty(SmsCodeUtils.sendMessage(phone))==true?-1:0;
    }

    @Override
    public TbUser login(String user, String pass) {
        return null;
    }

    @Override
    public TbUser saveUserByNameAndPhone(String phone, String username) {

        //检查用户是否存在
        TbUser user = userDao.selectByPhone(phone);
        if(user != null){
            user.setArea("0");//标识是否已经存在
            return user;
        }
        TbUser newUser = new TbUser();
        newUser.setUserName(username);
        newUser.setUid(UUID.randomUUID().toString());
        newUser.setMobileNumber(phone);
        //生成邀请码
        newUser.setInvitationCode(RandCodeUtils.generateShortUuid());
        newUser.setCreatedDate(new Date());
        userDao.insertSelective(newUser);
        return newUser;
    }

    /**
     * 投票
     * @param user
     * @param phone
     */
    @Override
    public void vote(TbUser user, String phone) {


       if(user.getStatus() !=null && user.getStatus() == 1){
            throw new RuntimeException("已经投过票了!");

       }else{
           TbUser u= userDao.selectByPhone(phone);
           if(u ==null){
               throw new RuntimeException("投的项目不存在!");
           }

           Integer score = u.getScore()==null? 1:u.getScore()+1;
           u.setScore(score);
           user.setStatus(1);
           userDao.updateByPrimaryKey(u);
           userDao.updateByPrimaryKey(user);

       }


    }

    @Override
    public List<TbUser> getList() {

      return  userDao.getList();

    }

    @Override
    public TbUser loginPhone(String phone, String code) {
        if(!AccountValidatorUtil.isMobile(phone)){
            throw new RuntimeException("手机号码错误!");
        }
        String tempCode = "1234";
        if (!tempCode.equals(code)){
            throw new RuntimeException("验证码错误");
        }
        //验证成功,检查用户是否存在，不存在就注册
        TbUser user = userDao.selectByPhone(phone);
        if(user != null){
            //直接返回user
            return user;
        }
        //新增一条记录
        TbUser newUser = new TbUser();
        newUser.setUserName(phone);
        newUser.setUid(UUID.randomUUID().toString());
        newUser.setMobileNumber(phone);
        //生成邀请码
        newUser.setInvitationCode(RandCodeUtils.generateShortUuid());
        newUser.setCreatedDate(new Date());
        userDao.insert(newUser);
        return user;
    }

    @Override
    public int getUserinfo(int id) {
        return 0;
    }

    @Override
    public int updateUserinfo(TbUser user) {

        return userDao.updateByPrimaryKey(user);


    }

    @Override
    public int register(TbUser user) {
        return 0;
    }
}
